package memory

import (
	"errors"
	"fmt"

	"gitee.com/gomaster/book-library/entity"
)

var userMap = make(map[string]*entity.UserInfo)

// MemUser  内存操作，用户信息
type MemUser struct {}

// UserAdd 添加用户
func (m *MemUser)UserAdd(us *entity.UserInfo) error {
	_, ok := userMap[us.Name]
	if ok {
		fmt.Printf("用户已存在")
		return errors.New("user exist already")
	}

	userMap[us.Name] = us
	return nil
}

// UserGet 查询用户
func (m *MemUser)UserGet(name string) *entity.UserInfo {
	us,ok := userMap[name]
	if !ok {
		fmt.Printf("用户不存在,userName:%s", name)
		return nil
	}
	return us
}

// UserDel 删除用户
func (m *MemUser)UserDel(name string) error {
	delete(userMap, name)
	return nil
}

// UserUpdate 更新用户
func (m *MemUser)UserUpdate(name string, bk *entity.UserInfo) error {
	_, ok := userMap[name]
	if !ok {
		fmt.Printf("user does not exist, name:%s", name)
		return errors.New("user does not exist")
	}
	userMap[name] = bk
	return nil
}

// UserList 查询用户列表
func (m *MemUser)UserList()[]*entity.UserInfo {
	return nil
}